<script setup>
  import { ref, onMounted } from 'vue'
  import messageApi from '@/apis/message'

  // 任务列表数据
  const notifyList = ref([])
  // 下一页页码
  const nextPage = ref(1)
  // 每页包含数据条数
  const pageSize = ref(5)
  // 是否为空列表
  const isEmpty = ref(false)
  // 是否还有更多数据
  const hasMore = ref(true)
  // 是否加载完成
  const isTriggered = ref(false)
  // 生命周期
  onMounted(() => {
    getNotifyList()
  })
  const read = async () => {
    const { code, data } = await messageApi.readAll(201)
    if (code !== 200) return uni.utils.toast('操作失败，稍后重试!')
    setTimeout(() => {
      uni.utils.toast('操作成功')
    }, 200)
    getNotifyList()
  }
  const getNotifyList = async (page = 1, pageSize = 10) => {
    try {
      const { code, data } = await messageApi.list(201, page, pageSize)
      // 清空原始的数据
      if (page === 1) notifyList.value = []
      console.log(data)
      if (code !== 200) return uni.utils.toast('获取通知失败 请稍后重试！')
      // 渲染数据
      notifyList.value = [...notifyList.value, ...(data.items || [])]
      // 更新下一页页码
      nextPage.value = ++data.page
      // 是否还有更多数据
      isEmpty.value = notifyList.value.length === 0
      hasMore.value = nextPage.value <= data.pages
    } catch (e) {
      //TODO handle the exception
    }
  }
  const onScrollViewRefresh = async () => {
    isTriggered.value = true
    await getNotifyList()
    isTriggered.value = false
  }
  const onScrollToLower = () => {
    if (!hasMore.value) return
    // 获取下一页数据
    getNotifyList(nextPage.value)
  }
</script>
<template>
  <scroll-view
    class="scroll-view"
    refresher-enabled
    @refresherrefresh="onScrollViewRefresh"
    @scrolltolower="onScrollToLower"
    :refresher-triggered="isTriggered"
    scroll-y
  >
    <view class="scroll-view-wrapper">
      <view class="message-action">
        <text class="iconfont icon-clear" @click="read"></text>
        全部已读
      </view>
      <uni-card
        v-for="notify in notifyList"
        :key="notify.id"
        :border="false"
        :is-shadow="false"
      >
        <view class="brief">{{ notify.content }}</view>
        <view class="extra">
          <text class="time">{{ notify.created }}</text>
          <navigator
            hover-class="none"
            class="link"
            :url="`/subpkg_task/detail/index?id=${notify.id}`"
            >查看详情</navigator
          >
        </view>
        <template v-slot:title>
          <view :class="{ unread: !notify.isRead }" class="title unread"
            >您有新的运输任务</view
          >
        </template>
      </uni-card>
      <view v-if="isEmpty" class="message-blank">暂无消息</view>
    </view>
  </scroll-view>
</template>

<style lang="scss" scoped>
  @import './styles.scss';
</style>
